package com.gm.wholesale.module.order.view;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.gm.commonlib.base.baseBean.BaseResp;
import com.gm.commonlib.base.baseView.IBaseView;
import com.gm.commonlib.interfaces.IResultListener;
import com.gm.commonlib.network.BaseHttpObserver;
import com.gm.commonlib.util.numberUtils.DigitUtil;
import com.gm.commonlib.util.numberUtils.NumberUtils;
import com.gm.commonlib.views.popupwindow.Popupwindow;
import com.gm.commonlib.views.toast.ToastUtil;
import com.gm.wholesale.R;
import com.gm.wholesale.constant.PayMethod;
import com.gm.wholesale.module.evenbus.EvenBusUtil;
import com.gm.wholesale.module.main.ui.MainActivity;
import com.gm.wholesale.module.order.bean.OrderDetailBean;
import com.gm.wholesale.module.order.bean.OrderPayInfoBean;
import com.gm.wholesale.module.settlement.bean.CustomerSettlementBean;
import com.gm.wholesale.module.settlement.model.ReconciliationMode;
import com.gm.wholesale.view.NumberInputView;

import java.util.HashMap;

public class RefundMoneyView extends LinearLayout {
    public static final int PAY_TYPE_UNPAY = 1; //未支付单子，进行支付
    public static final int PAY_TYPE_PARTIAL_PAYMENT = 2; //待结算进入
    private TextView allPriceTv;
    private ImageView closeView;
    private TextView arrearsTv;
    private TextView alreadyGetMoneyTv;
    private RadioGroup payMethodRg;
    private NumberInputView numberInputView;

    private EditText payPriceEd;

    private int payMethod;
    private Context mContext;
    private IResultListener<Boolean> listener;
    private CustomerSettlementBean mCustomerSettlementBean;

    public RefundMoneyView(Context context) {
        super(context);
        initView(context);
    }

    public RefundMoneyView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    MainActivity mActivity;

    public void setAct(MainActivity activity) {
        mActivity = activity;
    }

    private void initView(Context context) {
      View mainView= LayoutInflater.from(context).inflate(R.layout.view_refund, this);
      mainView.setOnClickListener(new OnClickListener() {
          @Override
          public void onClick(View v) {
              Popupwindow.hiddenPop();
          }
      });
        mContext = context;
        allPriceTv = findViewById(R.id.allPriceTv);
        closeView = findViewById(R.id.closeView);
        closeView.setOnClickListener(view -> Popupwindow.hiddenPop());
        arrearsTv = findViewById(R.id.arrearsTv);
        alreadyGetMoneyTv = findViewById(R.id.alreadyGetMoneyTv);
        payMethodRg = findViewById(R.id.payMethodRg);
        numberInputView = findViewById(R.id.numberInputView);
        payPriceEd = findViewById(R.id.payPriceEd);
        numberInputView.bindEdiText(payPriceEd, "");
        payMethodRg.setOnCheckedChangeListener((radioGroup, i) -> {
            if (i == R.id.payMethodWx) {
                payMethod = PayMethod.WX.getType();
            } else if (i == R.id.payMethodWxZfb) {
                payMethod = PayMethod.ZFB.getType();
            } else {
                payMethod = PayMethod.XJ.getType();
            }
        });
        numberInputView.setINumberView(() -> {
            String refnd = payPriceEd.getText().toString();
            if (TextUtils.isEmpty(refnd)) {
                ToastUtil.showToast("退款金额不能为空");
                return;
            }
            if (Double.parseDouble(refnd) <= 0) {
                ToastUtil.showToast("退款金额不能小于0");
                return;
            }
            refund(Double.parseDouble(refnd));
        });
    }

    private void refund(double money) {
        HashMap<String, Object> data = new HashMap<>();
        data.put("payMent", payMethod);
        data.put("payAmount", DigitUtil.parseDouble(money));
        data.put("version", mCustomerSettlementBean.getVersion());
        ReconciliationMode.payOrder((IBaseView) mContext, mCustomerSettlementBean.getId(), data, new BaseHttpObserver<BaseResp>() {
            @Override
            protected void handleResultOkResp(BaseResp resp) {
                listener.onResult(true);
            }
        });
    }


    public void showView(int type, CustomerSettlementBean customerSettlementBean, IResultListener<Boolean> iResultListener) {
        String payMoney;
        if (type==PAY_TYPE_UNPAY){
             payMoney = NumberUtils.getMoneyString(Math.abs(customerSettlementBean.getShouldPayAmount()));
        }
        else {
            payMoney = NumberUtils.getMoneyString(Math.abs(customerSettlementBean.getBalanceAmount()));
        }
        this.mCustomerSettlementBean = customerSettlementBean;
        this.listener = iResultListener;
        payMethod = PayMethod.WX.getType();
        allPriceTv.setText(NumberUtils.getMoneyString(customerSettlementBean.getOrderRefundAmount())+"元");
        payPriceEd.setText(payMoney);
        alreadyGetMoneyTv.setText("已退款: "+NumberUtils.getMoneyWithUnitStrig(Math.abs(customerSettlementBean.getPaidAmount())));
        arrearsTv.setText("未退款: " + payMoney+"元");
        numberInputView.bindEdiText(payPriceEd,"");
        payPriceEd.requestFocus();
    }

}
